package com.huping.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.huping.mapper.HomeworkMapper;
import com.huping.mapper.SampleCaseMapper;
import com.huping.pojo.*;
import com.huping.service.OJService;
import com.huping.utils.CMyCompiler;
import com.huping.utils.MyCompiler;
import com.huping.utils.CppMyCompiler;
import com.huping.utils.JavaComplier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Service
public class OJServiceImpl implements OJService {

    @Autowired
    HomeworkMapper homeworkMapper;

    @Autowired
    SampleCaseMapper sampleCaseMapper;

    @Override
    public Result teacherUpdate(JSONObject param) {
        Homework homework = new Homework();
        Integer homeworkId = param.getInteger("homeworkId");
        homework.setHomeworkId(homeworkId);
        homework.setName(param.getString("name"));
        homework.setProfile(param.getString("profile"));
        homework.setStartTime(LocalDateTime.parse(param.getString("startTime"), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        homework.setEndTime(LocalDateTime.parse(param.getString("endTime"), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        homework.setType(param.getString("type"));
        int r = homeworkMapper.homeworkUpdate(homework);
        if (r!=1) return Result.error("更新课程失败");
        sampleCaseMapper.sampleCaseDelete(param.getInteger("homeworkId"));
        JSONArray sampleCases=param.getJSONArray("sampleCase");
        List<SampleCase> sampleCaseList=new ArrayList<>();
        for(int i=1;i<=sampleCases.size();i++){
            JSONObject sampleCase = sampleCases.getJSONObject(i-1);
            sampleCaseList.add(new SampleCase(homeworkId,sampleCase.getString("input"),sampleCase.getString("output"),sampleCase.getString("type")));
        }

        sampleCaseList.forEach(sampleCase -> sampleCaseMapper.sampleCaseInsert(sampleCase));


        return Result.success();
    }

    @Override
    public Result studentUpdate(Integer gradeId, String code, String codeType) {
        Grade grade=new Grade();
        grade.setGradeId(gradeId);
        grade.setCode(code);
        grade.setStatus("0");
        grade.setCodeType(codeType);
        int r = sampleCaseMapper.updateSampleCase(grade);
        if(r==1) return Result.success();
        else return Result.error("更新失败");
    }

    @Override
    public Result studentSubmit(Integer gradeId, String code, String codeType )throws IOException, InterruptedException {
        System.out.println(code);
        Grade grade=new Grade();
        grade.setGradeId(gradeId);
        grade.setCode(code);
        grade.setStatus("1");
        grade.setCodeType(codeType);
        int r = sampleCaseMapper.updateSampleCase(grade);
        if(r!=1) return Result.error("更新失败");
        //计算程序结果
        MyCompiler myCompiler =null;
        switch (codeType){
            case "0":
                myCompiler =new JavaComplier();
                break;
            case "1":
                myCompiler =new CppMyCompiler();
                break;
            case "2":
                myCompiler =new CMyCompiler();
                break;
        }
        List<SampleCase> sampleCaseList =sampleCaseMapper.getSampleCaseByGradeId(gradeId);
        List<String> inputCasesList = new ArrayList<>();
        for (SampleCase sampleCase : sampleCaseList){
            inputCasesList.add(sampleCase.getInput());
        }
        r = myCompiler.start(code,inputCasesList);
        List<OJResult> ojResultList=new ArrayList<>();
        if (r==1){
            ojResultList.add(new OJResult("0", myCompiler.getErrorResult(), null, null, null,null));
        }else {
            for(int i=0;i<inputCasesList.size();i++){
                String actualOutput = myCompiler.getInputResult().get(i);
                if(actualOutput.equals("timeout")){
                    OJResult ojResultTemp=new OJResult("3", null,sampleCaseList.get(i).getInput(),sampleCaseList.get(i).getOutput(), null,sampleCaseList.get(i).getType());
                    ojResultList.add(ojResultTemp);
                }
                else{
                    actualOutput=actualOutput.replaceAll("^\\s+|\\s+$", "");
                    if(actualOutput.equals(sampleCaseList.get(i).getOutput())){
                        OJResult ojResultTemp=new OJResult("1", null,sampleCaseList.get(i).getInput(),sampleCaseList.get(i).getOutput(), actualOutput,sampleCaseList.get(i).getType());
                        ojResultList.add(ojResultTemp);
                    }else {
                        OJResult ojResultTemp=new OJResult("2", null,sampleCaseList.get(i).getInput(),sampleCaseList.get(i).getOutput(), actualOutput,sampleCaseList.get(i).getType());
                        ojResultList.add(ojResultTemp);
                    }

                }

            }
        }
        JSONArray ojResultJSONArray = JSONArray.parseArray(JSON.toJSONString(ojResultList));
        return Result.success(ojResultJSONArray);

    }
}
